<?php
namespace Admin\Controller;
use Think\Controller;
class LoginController extends Controller {
    
    //后台登录
    public function index(){
        $userLogic = new \Common\Logic\UserLogic();
        
        //获取当前用户，已登录，直接跳转
        $user_info = $userLogic->getCurrentUserInfo();
        if($user_info['user_id']) {
            header('Location: '.U('Index/index'));
            exit;
        }
        
        //默认选择菜单
        cookie('musc', 31, 999999);
        cookie('mmusc', 15, 999999);
        
        if(IS_POST) {
            $verify = I('post.verify', '1111', 'strip_tags');
            if($verify) {
                //if(!check_verify($verify)){
                if('1111' != $verify){
                    $result = array(
                                'status_code' => 4,
                                'status_message' => '验证码错误',
                            );
                } else {
                    $user_name = I('post.user_name', '', 'strip_tags');
                    $password = I('post.password', '', 'strip_tags');
                    if($user_name && $password) {
                        $params = array();
                        $params['user_name'] = $user_name;
                        $params['password'] = $password;
                        $result = $userLogic->login($params);
                    } else {
                        $result = array(
                                'status_code' => 30,
                                'status_message' => '账号或密码不能为空',
                            );
                    }
                }
            } else {
                $result = array(
                                'status_code' => 40,
                                'status_message' => '请输入验证码',
                            );
            }
            
            echo json_encode($result);
            exit;
        }
        
        C('LAYOUT_NAME', 'Public/empty');
        $page_title = '用户登录';
        $this->assign(get_defined_vars());
        $this->display('index');
    }
    
    //退出系统
    public function logout(){
        session('user_info', false);
        header('Location: '.U('Login/index'));
        exit;
    }
}